package com.urick.loadbanlance.dubbo;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Design Theories:
 * <a href=""></a>
 *
 * @author uRick 2021/12/13 18:01
 * Email: rickwork@163.com
 * Wechat: yy3076815642
 * QQ: 3076815642
 * @since 1.0
 */
public class ServerInstance {
    /**
     * 服务唯一ID,一个服务一个唯一ID，自动生成,默认UUID
     * <p>
     * 注：集群中相同服务，不同实例uid相同
     */
    private AtomicInteger count = new AtomicInteger();

    private String uid = StrUtil.uuid();
    /**
     * 服务IP
     */
    private String ip;
    /**
     * 服务端口
     */
    private int port;
    /**
     * 服务权重
     */
    private int weight;
    /**
     * 服务启动赶时间戳
     */
    private long startTime;
    /**
     * 服务启动预热时间阀值
     */
    private long warmup;
    /**
     * 服务是否可用
     */
    private boolean available;

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getWarmup() {
        return warmup;
    }

    public void setWarmup(long warmup) {
        this.warmup = warmup;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }

    public AtomicInteger getCount() {
        return count;
    }

    public String toIdentityString() {
        return ip + port + weight;
    }
}
